package com.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.beans.DialBean;
import com.beans.LocationBean;
import com.beans.SmsBean;
import com.utils.DBUtility;
import com.utils.ListFactory;

public class CheckInfoByJudgeServlet extends HttpServlet {

	private Connection conn;
	private PreparedStatement pst;
	private ResultSet rs;

	/**
	 * 
	 */
	private static final long serialVersionUID = -6475002225692615950L;




	public CheckInfoByJudgeServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException {

		this.doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException {
		
		String path = "";
		String userid = (String)request.getSession().getAttribute("userid");
		
		String type = request.getParameter("type");

		if( type.equals("sms")){
			System.out.println("[CheckInfoByJudgeServlet]go to sms");
			path = "/checkDetailSMSInfo.jsp";
			List<SmsBean> smsList = this.getSMSList(userid);
			
			smsList = ListFactory.manufactureSMSList( smsList );
			
			request.setAttribute("list", smsList );
			
			request.getRequestDispatcher( path ).forward(request, response);

		}else if( type.equals("dial")){

			path = "/checkDetailDialInfo.jsp";
			List<DialBean> dialList = this.getDialList(userid);
			
			dialList = ListFactory.manufactureDialList( dialList );
			request.setAttribute("list", dialList );
			
			request.getRequestDispatcher( path ).forward(request, response);
			
		}else if( type.equals("location")){
			path = "/checkLocationInfo.jsp";
			List<LocationBean> locationList = this.getLocationList(userid);
			
			locationList = ListFactory.manufactureLocationList( locationList );
			
			request.setAttribute("list", locationList );
			
			request.getRequestDispatcher( path ).forward(request, response);

		}
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}


	/**
	 * @param userid
	 * @return
	 */
	private List<SmsBean> getSMSList( String userid ){
		List<SmsBean> list = new ArrayList<SmsBean>();

		String sql = "select msg,type,id,add_time from db.jiankong_data_info_table  where controlled_id= ? and msg != '' ORDER BY id DESC";

		conn = DBUtility.getMySqlConnection();

		try {
			pst = conn.prepareStatement( sql );
			pst.setString( 1, userid );

			rs = pst.executeQuery();

			while (rs.next()) {

				SmsBean bean = new SmsBean();

				bean.setSms( rs.getString("msg") );
				bean.setType( rs.getString("type") );
				bean.setAdd_time( rs.getString("add_time") );
				bean.setId( rs.getInt("id"));

				list.add( bean );

			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{

			closeAllConnections();

		}
		return list;
	}

	/**
	 * @param userid
	 * @return
	 */

	private List<DialBean> getDialList( String userid ){

		List<DialBean> list = new ArrayList<DialBean>();

		String sql = "select dial,type,id,add_time from db.jiankong_data_info_table  where controlled_id= ? and dial != '' ORDER BY id DESC";

		conn = DBUtility.getMySqlConnection();

		try {
			pst = conn.prepareStatement( sql );
			pst.setString( 1, userid );

			rs = pst.executeQuery();

			while (rs.next()) {

				DialBean bean = new DialBean();

				bean.setDial( rs.getString("dial") );
				bean.setType( rs.getString("type") );
				bean.setAdd_time( rs.getString("add_time") );
				bean.setId( rs.getInt("id"));

				list.add( bean );

			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{

			closeAllConnections();

		}
		return list;

	}

	/**
	 * @param userid
	 * @return
	 */
	private List<LocationBean> getLocationList( String userid ){

		List<LocationBean> list = new ArrayList<LocationBean>();

		String sql = "select location,type,id,add_time from db.jiankong_data_info_table  where controlled_id= ? and location != '' ORDER BY id DESC";

		conn = DBUtility.getMySqlConnection();

		try {
			pst = conn.prepareStatement( sql );
			pst.setString( 1, userid );

			rs = pst.executeQuery();

			while (rs.next()) {

				LocationBean bean = new LocationBean();

				bean.setLocation( rs.getString("location") );
				bean.setType( rs.getString("type") );
				bean.setAdd_time( rs.getString("add_time") );
				bean.setId( rs.getInt("id"));

				list.add( bean );

			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{

			closeAllConnections();

		}
		return list;

	}

	/**
	 */
	private void closeAllConnections(){

		if( rs != null ){

			try {
				rs.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}

		if( pst != null){

			try {
				pst.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}


	}

}
